package com.jmodata.zxu.service.impl.manager;

import com.jmodata.zxu.dao.MyBatisCommonDAO;
import com.jmodata.zxu.model.DataGrid;
import com.jmodata.zxu.pojo.manager.ManagerRole;
import com.jmodata.zxu.pojo.manager.Role;
import com.jmodata.zxu.pojo.manager.RoleMenu;
import com.jmodata.zxu.service.manager.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("roleService")
@Transactional(rollbackFor = Exception.class)
public class RoleServiceImpl implements RoleService {

	@Autowired
	private MyBatisCommonDAO myBatisCommonDAO;
	
	@Override
	public int insertRole(Role role) throws Exception {
		int flag = 0;
		try {
			flag = myBatisCommonDAO.executeInsert("role.insertRole",role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int updateRole(Role role) throws Exception {
		int flag = 0;
		try {
			flag = myBatisCommonDAO.executeUpdate("role.updateRole",role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public int deleteRole(BigDecimal roleid) throws Exception {
		int flag = 0;
		try {
			flag = myBatisCommonDAO.executeDelete("role.deleteRole",roleid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	@Override
	public DataGrid queryRoleList(Map<Object, Object> map) throws Exception {
		DataGrid dg = new DataGrid();
		List<Role> roleList =  myBatisCommonDAO.executeForObjectList("role.queryRoleList", map);
		dg.setRows(roleList);
		dg.setTotal(myBatisCommonDAO.executeForObjectListCount("role.queryRoleListCount", map));
		return dg;
	}

	@Override
	public Role getRoleById(String roleid) throws Exception {
		Role role =null;
		if(null != roleid && !"".equals(roleid)){
			role = myBatisCommonDAO.executeForObject("role.getRoleById", roleid, Role.class);
		}
		return role;
	}

	@Override
	public int insertRoleMenu(List<RoleMenu> list, String roleId) throws Exception {
		myBatisCommonDAO.executeDelete("menu.deleteRoleMenu",roleId);
		int flag = 0;
		if(null != list && list.size()>0){
			flag = myBatisCommonDAO.executeInserts("role.insertRoleMenu", list);
		}
		return flag;
	}

	@Override
	public List<RoleMenu> queryRoleMenu(String roleid) throws Exception {
		List<RoleMenu> list = new ArrayList<RoleMenu>();
		list = myBatisCommonDAO.executeForObjectList("role.queryRoleMenu", roleid);
		return null != list && list.size()>0?list:null;
	}

	@Override
	public int deleteRoleMenu(String roleid) throws Exception {
		int flag = myBatisCommonDAO.executeDelete("menu.deleteRoleMenu",roleid);
		return flag;
	}

	@Override
	public List<ManagerRole> findRoleIdByManagerId(Map<Object, Object> map)
			throws Exception {
		List<ManagerRole> list=myBatisCommonDAO.executeForObjectList("role.findRoleIdByManagerId", map);
		return list;
	}

	@Override
	public List<Role> getRolesList() {
		return myBatisCommonDAO.executeForObjectList("role.getRolesList", null);
	}

}
